package com.longrise.android.byjk.plugins.hra.hratabthird;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haibin.calendarview.Util;
import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.event.HraDateEvent;
import com.longrise.android.byjk.plugins.hra.hratabfirst.CalendarDialogFragment;
import com.longrise.android.byjk.plugins.hra.hratabfirst.OrderPlaceChildBean;
import com.longrise.android.byjk.plugins.hra.hratabsecond.MineAppointDetailActivity;
import com.longrise.android.byjk.utils.CheckCard;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.PhoneUtils;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/1/9.
 * 我的改签--预约改签
 */

public class HraOrderEndorseActivity extends BaseActivity2<HraOrderEndorsePresenter> implements HraOrderEndorseContract.View, View.OnClickListener, TextWatcher{

    private static final String TAG = "HraOrderEndorseActivity";
    public static final String MINEENDORSEBEAN = "mineendorsebean";
    public static final String ENDORSESEARCHBEAN = "endorsesearchbean";
    public static final int HRAORDERENDORSE = 2;
    private EntityBean mineEndorseBean = new EntityBean();
    private RelativeLayout rl_har_endorse_area;
    private RelativeLayout rl_hra_endorse_type;
    private RelativeLayout rl_hra_endorse_date;
    private RelativeLayout rl_hra_endorse_name;
    private RelativeLayout rl_hra_endorse_sex;
    private RelativeLayout rl_hra_endorse_card;
    private RelativeLayout rl_hra_endorse_tel;
    private TextView hra_service_point;
    private TextView hra_service_area;
    private TextView hra_service_tel;
    private TextView hra_service_type;
    private TextView hra_service_date;
    private TextView hra_endorse_name;
    private TextView hra_endorse_sex;
    private TextView hra_endorse_card;
    private TextView hra_endorse_phone;

    private Dialog mOrderInfoDialog;
    private Dialog mOrderSexDialog;

    private String dialogType = "";
    private String nameType = "1";
    private String cardType = "2";
    private String telType = "3";
    private EditText et_dialog_info;
    private TextView tv_confirm;
    private Button mBtnConfirm;

    private int mYear;
    private int mMonth;
    private int mDay;
    private int uploadType;   //用于区分是修改检测人信息还是服务网点信息
    private String id;
    private String endorse;
    private RelativeLayout rl_hra_endorse_time;
    private TextView hra_service_time;
    private String bespeakdate;
    private String storeid;
//    private String machineid;
    private String examinedate;
    private List<OrderPlaceChildBean> mBeanList;
    private String timeslot1;
    private String timeslot2;
    private String payprice;
    private int flags;
    private String orderName;
    private String address;
    private String storephone;
    private String machinemodelname;
    private String timeslot = "";
    private String username;
    private String sex;
    private String cardno;
    private String usermobile;
    private String machinemodel;
    private String timeid;
    private String areaid;
    private String timeslotid1;
    private String timeslotid2;
    private String sexType;
    private String orderDate;
    private String mOrderId;
    private String checkDate;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_hra_order_endorse;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        rl_har_endorse_area = (RelativeLayout) findViewById(R.id.rl_har_endorse_area);
        rl_hra_endorse_type = (RelativeLayout) findViewById(R.id.rl_hra_endorse_type);
        rl_hra_endorse_date = (RelativeLayout) findViewById(R.id.rl_hra_endorse_date);
        rl_hra_endorse_name = (RelativeLayout) findViewById(R.id.rl_hra_endorse_name);
        rl_hra_endorse_sex = (RelativeLayout) findViewById(R.id.rl_hra_endorse_sex);
        rl_hra_endorse_card = (RelativeLayout) findViewById(R.id.rl_hra_endorse_card);
        rl_hra_endorse_tel = (RelativeLayout) findViewById(R.id.rl_hra_endorse_tel);
        hra_service_point = (TextView) findViewById(R.id.tv_hra_endorse_point);
        hra_service_area = (TextView) findViewById(R.id.tv_mine_endorse_area);
        hra_service_tel = (TextView) findViewById(R.id.hra_endorse_tel);
        hra_service_type = (TextView) findViewById(R.id.tv_hra_endorse_type);
        hra_service_date = (TextView) findViewById(R.id.tv_hra_endorse_date);
        hra_endorse_name = (TextView) findViewById(R.id.tv_hra_endorse_name);
        hra_endorse_sex = (TextView) findViewById(R.id.tv_hra_endorse_sex);
        hra_endorse_card = (TextView) findViewById(R.id.tv_hra_endorse_card);
        hra_endorse_phone = (TextView) findViewById(R.id.tv_hra_endorse_tel);
        mBtnConfirm = (Button) findViewById(R.id.btn_endorse_confirm);
        rl_hra_endorse_time = (RelativeLayout) findViewById(R.id.rl_hra_endorse_time);
        hra_service_time = (TextView) findViewById(R.id.tv_hra_endorse_time);
        Control.setBookDataId("");
        setToolbarTitle(AppUtil.getString(R.string.order_sign));
        mBtnConfirm.setEnabled(false);
        getExtraData();
        initEvent();
        setData2View();
    }

    public void getExtraData(){
        Intent intent = getIntent();
        flags = intent.getFlags();
        switch (flags){
            case MineEndorseFragment.ENDORSEFRAGMENT:
                String s1 = intent.getStringExtra(MINEENDORSEBEAN);
                mineEndorseBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(s1, EntityBean.class);
                break;

            case EndorseSearchActivity.ENDORSESEARCH:
                String s2 = intent.getStringExtra(ENDORSESEARCHBEAN);
                mineEndorseBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(s2, EntityBean.class);
                break;
        }
    }

    //设置数据到view
    public void setData2View(){
        orderName = mineEndorseBean.getString("storename");
        address = mineEndorseBean.getString("address");
        storephone = mineEndorseBean.getString("storephone");
        machinemodelname = mineEndorseBean.getString("machinemodelname");
        examinedate = mineEndorseBean.getString("examinedate");
        orderDate = mineEndorseBean.getString("examine");
        timeslot = mineEndorseBean.getString("timeslot");
        username = mineEndorseBean.getString("username");
        sex = mineEndorseBean.getString("sexname");
        cardno = mineEndorseBean.getString("cardno");
        usermobile = mineEndorseBean.getString("usermobile");
        id = mineEndorseBean.getString("id");
        storeid = mineEndorseBean.getString("storeid");
        endorse = mineEndorseBean.getString("endorse");
//        machineid = mineEndorseBean.getString("machineid");
        payprice = mineEndorseBean.getString("payprice");
        machinemodel = mineEndorseBean.getString("machinemodel");
        timeid = mineEndorseBean.getString("timeid");
        areaid = mineEndorseBean.getString("areaid");
        mOrderId = mineEndorseBean.getString("orderid");

        hra_service_point.setText(orderName);
        hra_service_area.setHint(address);
        hra_service_tel.setHint(storephone);
        hra_service_type.setHint("HRA "+machinemodelname);
        Control.setOrderTimeslot(timeslot);
        Control.setOrderName(username);
        Control.setOrderSex(sex);
        Control.setOrderCardNum(cardno);
        Control.setOrderPhone(usermobile);
        hra_endorse_name.setHint(Control.getOrderName());
        hra_endorse_sex.setHint(Control.getOrderSex());
        hra_endorse_card.setHint(Control.getOrderCardNum());
        hra_service_time.setHint(timeslot);
        PrintLog.e(TAG,"timeslot=:"+timeslot);
        Control.setOrderDate(orderDate);
        PrintLog.e(TAG,"orderDate=:"+Control.getOrderDate());
        mPresenter.getTimeslotData(storeid,Control.getOrderDate());
        hra_service_date.setHint(Control.getOrderDate());

    }


    public void initEvent(){
        mBtnConfirm.setOnClickListener(this);
        rl_har_endorse_area.setOnClickListener(this);
        rl_hra_endorse_type.setOnClickListener(this);
        rl_hra_endorse_date.setOnClickListener(this);
        rl_hra_endorse_name.setOnClickListener(this);
        rl_hra_endorse_sex.setOnClickListener(this);
        rl_hra_endorse_card.setOnClickListener(this);
        rl_hra_endorse_tel.setOnClickListener(this);
        hra_endorse_name.addTextChangedListener(this);
        hra_endorse_sex.addTextChangedListener(this);
        hra_endorse_card.addTextChangedListener(this);
        hra_endorse_phone.addTextChangedListener(this);
        hra_service_time.addTextChangedListener(this);
        hra_service_date.addTextChangedListener(this);
        rl_hra_endorse_time.setOnClickListener(this);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击修改服务点信息
            case R.id.rl_har_endorse_area:
                jump2HraServiceDetail();
                break;
            //点击修改预约日期
            case R.id.rl_hra_endorse_date:
                toShowCalendar();
                break;

            //点击修改预约时间段
            case R.id.rl_hra_endorse_time:
                showTimeslotDialog();
                break;

            //点击修改预约姓名
            case R.id.rl_hra_endorse_name:
                dialogType = nameType;
                showOrderInfoDialog(dialogType);
                break;
            //点击修改预约性别
            case R.id.rl_hra_endorse_sex:
                showSexDialog();
                break;
            //点击修改预约身份证号
            case R.id.rl_hra_endorse_card:
                dialogType = cardType;
                showOrderInfoDialog(dialogType);
                break;
            //点击修改预约手机号码
            case R.id.rl_hra_endorse_tel:
                dialogType = telType;
                showOrderInfoDialog(dialogType);
                break;
            //点击确认修改
            case R.id.btn_endorse_confirm:
                //上传到服务器
                if(uploadType == 1){
                    uploadCheckerInfo();
                }else if(uploadType == 2){   //修改预约时间
                    uploadEndorseTimeInfo();
                }
                break;
        }
    }

    //上传修改用户基本信息
    public void uploadCheckerInfo(){
        EntityBean bean = new EntityBean();
        bean.set("id",id);
        bean.set("cardno", Control.getOrderCardNum());
        bean.set("usermobile",Control.getOrderPhone());
        bean.set("username",Control.getOrderName());
        bean.set("sex",getSexType(Control.getOrderSex()));
        bean.set("endorse",endorse);
        mPresenter.uploadCheckInfo(bean);   //修改用户基本信息
    }

    //上传修改时间信息
    public void uploadEndorseTimeInfo(){
        EntityBean bean = new EntityBean();
        EntityBean customerbean = new EntityBean();
        customerbean.set("storeid", storeid);
//        customerbean.set("machineid", machineid);
        customerbean.set("timeid",Control.getOrderTimeslotId() );      //时间段id
        customerbean.set("areaid", areaid);
        customerbean.set("endorse", endorse);
        customerbean.set("usermobile", Control.getOrderPhone());
        customerbean.set("sex", getSexType(Control.getOrderSex()));
        customerbean.set("address", address);
        customerbean.set("storename", orderName);
        customerbean.set("machinemodel", machinemodel);
        customerbean.set("username", Control.getOrderName());
        customerbean.set("cardno",Control.getOrderCardNum());
        customerbean.set("examinedate", Control.getOrderDate());
        customerbean.set("timeslot", Control.getOrderTimeslot());

        bean.set("oldcustomerid",id);
        bean.set("oldtimeid",timeid);
        bean.set("customerbean",customerbean);
        mPresenter.uploadEndorseTimeInfo(bean);

    }


    public void jump2HraServiceDetail() {

        HraCheckerBean bean = new HraCheckerBean();
        bean.setUrseName(Control.getOrderName());
        bean.setSex(Control.getOrderSex());
        bean.setCardNum(Control.getOrderCardNum());
        bean.setPhoneNum(Control.getOrderPhone());
        Intent intent = new Intent(mContext, ChangeHraPointActivity.class);
        intent.putExtra(ChangeHraPointActivity.PAYPRICE,payprice);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(ChangeHraPointActivity.CHECKBEAN,bean);
        intent.putExtras(mBundle);
        startActivity(intent);
    }


    public String getSexType(String sex){

        if("男".equals(sex)){
            sexType = "0";
        }else if("女".equals(sex)){
            sexType = "1";
        }
        return sexType;
    }
    /**
     * 显示日历
     */
    private void toShowCalendar() {
        CalendarDialogFragment fragment = CalendarDialogFragment.newInstance(mYear, mMonth, mDay,2);
        fragment.show(getSupportFragmentManager(), "hradialogfragment");
    }

    /**
     * 设置head 和覆盖view上面的日期
     */
    private void setDate(int year,int month, int day) {
        String date = year+"-"+month + "-" + day;
        checkDate = Util.getMyDate(date);
        Control.setOrderDate(checkDate);
        hra_service_date.setText(checkDate);
    }

    /**
     * 获取 选择到的日期
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeEndorseDateEvent(HraDateEvent event) {
        int pageNum = event.getPageNum();
        if(2 == pageNum){
            mYear = event.getYear();
            mMonth = event.getMonth();
            mDay = event.getDay();
            setDate(mYear,mMonth, mDay);
            String time = mYear + "-" + mMonth + "-" + mDay + "";
            bespeakdate = Util.getMyDate(time);
            mPresenter.getTimeslotData(storeid,bespeakdate);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registEndorseEvent(EndorseRefreshEvent event){
        if(event.isClose()){ //关闭当前界面
            finish();
        }
    }

    //弹出填写姓名,身份证,手机号码的dialog
    public void showOrderInfoDialog(final String type){
        final View mEditDialog = View.inflate(mContext, R.layout.dialog_invoice_edit, null);
        final TextView tv_dialog_title = (TextView) mEditDialog.findViewById(R.id.tv_dialog_title);
        final TextView tv_dialog_notice = (TextView) mEditDialog.findViewById(R.id.tv_dialog_notice);
        et_dialog_info = (EditText) mEditDialog.findViewById(R.id.et_dialog_info);
        TextView tv_cancel = (TextView) mEditDialog.findViewById(R.id.tv_cancel);
        tv_confirm = (TextView) mEditDialog.findViewById(R.id.tv_confirm);
        final LinearLayout ll_recipient_content  = (LinearLayout)mEditDialog.findViewById(R.id.ll_recipient_content);
        final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ll_recipient_content.getLayoutParams();

        //姓名
        if( type == nameType){
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.test_name));
            tv_dialog_notice.setVisibility(View.GONE);
            mOrderInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);

            et_dialog_info.setFilters(new InputFilter[]{
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                                   int dstart, int dend) {

                            if(source.toString().matches("[\u4e00-\u9fa5]+")){
                                tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
                                return source;
                            }
                            return "";
                        }
                    }
            });

            String tv_order_name = Control.getOrderName();
            if(!TextUtils.isEmpty(tv_order_name)){
                et_dialog_info.setText(tv_order_name);
                et_dialog_info.setSelection(tv_order_name.length());
            }
        }else if( type == cardType ){    //身份证
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.test_card));
            tv_dialog_notice.setVisibility(View.GONE);
            mOrderInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            String tv_order_card = Control.getOrderCardNum();
            if(!TextUtils.isEmpty(tv_order_card)){
                et_dialog_info.setText(tv_order_card);
                et_dialog_info.setSelection(tv_order_card.length());
            }
        }else if( type == telType ){    //手机号码
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.test_tel));
            tv_dialog_notice.setVisibility(View.GONE);
            mOrderInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            et_dialog_info.setInputType(InputType.TYPE_CLASS_NUMBER);
            String tv_order_tel = Control.getOrderPhone();
            if(!TextUtils.isEmpty(tv_order_tel)){
                et_dialog_info.setText(tv_order_tel);
                et_dialog_info.setSelection(tv_order_tel.length());
            }
        }

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String dialigInfo = et_dialog_info.getText().toString();
                if(!TextUtils.isEmpty(dialigInfo)){
                    if( type == nameType){
                        hra_endorse_name.setText(dialigInfo);
                        Control.setOrderName(dialigInfo);
                        DialogUtil.getInstance().dismiss();
                    }else if( type == cardType ){
                        try {
                            if(CheckCard.IDCardValidate(dialigInfo)){
                                hra_endorse_card.setText(dialigInfo);
                                Control.setOrderCardNum(dialigInfo);
                                DialogUtil.getInstance().dismiss();
                            }else {
                                showToast(getString(R.string.cardno_type_error));
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                    }else if(type == telType){
                        if (PhoneUtils.isPhoneNumber(et_dialog_info.getText().toString())) {
                            hra_endorse_phone.setText(dialigInfo);
                            Control.setOrderPhone(dialigInfo);
                            DialogUtil.getInstance().dismiss();
                        }else {
                            showToast(getString(R.string.tel_hint));
                        }
                    }
                }
            }
        });
        uploadType = 1;
        et_dialog_info.addTextChangedListener(this);
        mOrderInfoDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        mOrderInfoDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    //弹出选择性别的dialog
    public void showSexDialog(){
        View mContentDialog = View.inflate(mContext, R.layout.dialog_sex_select, null);
        final RadioButton rb_sex_male = (RadioButton) mContentDialog.findViewById(R.id.rb_sex_male);
        final RadioButton rb_sex_female = (RadioButton) mContentDialog.findViewById(R.id.rb_sex_female);
        RelativeLayout rl_content_item1= (RelativeLayout) mContentDialog.findViewById(R.id.rl_sex_item1);
        RelativeLayout rl_content_item2= (RelativeLayout) mContentDialog.findViewById(R.id.rl_sex_item2);
        String tv_order_sex = Control.getOrderSex();
        if(!TextUtils.isEmpty(tv_order_sex)){
            if(tv_order_sex.equals(AppUtil.getString(R.string.order_sex_male))){
                rb_sex_male.setChecked(true);
            }else if(tv_order_sex.equals(AppUtil.getString(R.string.order_sex_female))){
                rb_sex_female.setChecked(true);
            }
        }
        rl_content_item1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_sex_male.setChecked(true);
            }
        });

        rl_content_item2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_sex_female.setChecked(true);
            }
        });


        mOrderSexDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mContentDialog, 311, 183);
        mOrderSexDialog.setCanceledOnTouchOutside(true);
        rb_sex_male.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                hra_endorse_sex.setText(AppUtil.getString(R.string.order_sex_male));
                Control.setOrderSex(AppUtil.getString(R.string.order_sex_male));
            }
        });

        rb_sex_female.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                hra_endorse_sex.setText(AppUtil.getString(R.string.order_sex_female));
                Control.setOrderSex(AppUtil.getString(R.string.order_sex_female));
            }
        });
        uploadType = 1;
    }

    //弹出选择时间段的dialog
    public void showTimeslotDialog(){
        View mContentDialog = View.inflate(mContext, R.layout.dialog_sex_select, null);
        final RadioButton rb_sex_male = (RadioButton) mContentDialog.findViewById(R.id.rb_sex_male);
        final RadioButton rb_sex_female = (RadioButton) mContentDialog.findViewById(R.id.rb_sex_female);
        RelativeLayout rl_content_item1= (RelativeLayout) mContentDialog.findViewById(R.id.rl_sex_item1);
        RelativeLayout rl_content_item2= (RelativeLayout) mContentDialog.findViewById(R.id.rl_sex_item2);
        TextView tv_timeslot1 = (TextView) mContentDialog.findViewById(R.id.tv_content_type1);
        TextView tv_timeslot2 = (TextView) mContentDialog.findViewById(R.id.tv_content_type2);
        if(mBeanList!= null){
            timeslot1 = mBeanList.get(0).getTimeslot();
            timeslot2 = mBeanList.get(1).getTimeslot();
            timeslotid1 = mBeanList.get(0).getTimeslotid();
            timeslotid2 = mBeanList.get(1).getTimeslotid();
            tv_timeslot1.setText(timeslot1);
            tv_timeslot2.setText(timeslot2);
            String tv_timeslot = hra_service_time.getText().toString();
            PrintLog.e(TAG,"tv_timeslot=:"+Control.getOrderTimeslot());
            if(!TextUtils.isEmpty(Control.getOrderTimeslot())){
                if(Control.getOrderTimeslot().equals(timeslot1)){
                    rb_sex_male.setChecked(true);
                    PrintLog.e(TAG,"===111111111111111======");
                }else if(Control.getOrderTimeslot().equals(timeslot2)){
                    rb_sex_female.setChecked(true);
                    PrintLog.e(TAG,"===2222222222222222======");
                }
            }
        }
        rl_content_item1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                rb_sex_male.setChecked(true);
                uploadType = 2;
            }
        });

        rl_content_item2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                rb_sex_female.setChecked(true);
                uploadType = 2;
            }
        });


        mOrderSexDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mContentDialog, 311, 183);
        mOrderSexDialog.setCanceledOnTouchOutside(true);
        rb_sex_male.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                hra_service_time.setText(timeslot1);
                Control.setOrderTimeslot(timeslot1);
                Control.setOrderTimeslotId(timeslotid1);
                uploadType = 2;
            }
        });

        rb_sex_female.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                hra_service_time.setText(timeslot2);
                Control.setOrderTimeslot(timeslot2);
                Control.setOrderTimeslotId(timeslotid2);
                uploadType = 2;
            }
        });

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        if(orderDate.equals(Control.getOrderDate())){
            hra_service_date.setTextColor(Color.parseColor("#FFB3B3B3"));
            String s1 = hra_service_time.getText().toString();
            if(timeslot.equals(s1)){
                hra_service_time.setTextColor(Color.parseColor("#FFB3B3B3"));
                mBtnConfirm.setEnabled(false);
            }else{
                hra_service_time.setTextColor(Color.parseColor("#FFFBA140"));
                mBtnConfirm.setEnabled(true);
            }
        }else{
            if("无可预约时间段".equals(hra_service_time.getText().toString())){
                mBtnConfirm.setEnabled(false);
                PrintLog.e(TAG,"===33333333333333333======");
            }else{
                hra_service_date.setTextColor(Color.parseColor("#FFFBA140"));
                hra_service_time.setTextColor(Color.parseColor("#FFFBA140"));
                mBtnConfirm.setEnabled(true);
                PrintLog.e(TAG,"===44444444444444444======");
            }
        }

        if(dialogType == nameType||dialogType == telType||dialogType == cardType){
            if(TextUtils.isEmpty(s)){
                tv_confirm.setTextColor(Color.parseColor("#FF818181"));
            }else {
                tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void refreshStockData(EntityBean bean) {

        EntityBean[] dataBean = bean.getBeans("result");
        mBeanList = new ArrayList<>();
        if (dataBean != null) {
            mBeanList.clear();
            for (EntityBean resultbean : dataBean) {
                String timeslot = resultbean.getString("timeslot");
                String stockpeople = resultbean.getString("stockpeople");
                String timeslotid = resultbean.getString("id");
                OrderPlaceChildBean childBean = new OrderPlaceChildBean();
                childBean.setTimeslot(timeslot);
                childBean.setStockpeople(stockpeople);
                childBean.setTimeslotid(timeslotid);
                mBeanList.add(childBean);
            }
            PrintLog.e(TAG,mBeanList.toString());
        } else {
            hra_service_time.setPadding(0, 0, AppUtil.dip2px(16), 0);
            hra_service_time.setCompoundDrawables(null,null,null,null);
            hra_service_time.setText("无可预约时间段");
            rl_hra_endorse_time.setClickable(false);
        }

        if(mBeanList.size() == 0){
            hra_service_time.setPadding(0, 0, AppUtil.dip2px(16), 0);
            hra_service_time.setCompoundDrawables(null,null,null,null);
            hra_service_time.setText("无可预约时间段");
            rl_hra_endorse_time.setClickable(false);
            PrintLog.e(TAG,"===55555555555555555======");
        }else {
            if(mBeanList.size() == 1){
                hra_service_time.setPadding(0, 0, AppUtil.dip2px(16), 0);
                hra_service_time.setCompoundDrawables(null,null,null,null);
                String timeslot1 = mBeanList.get(0).getTimeslot();
                hra_service_time.setText(timeslot1);
                Control.setOrderTimeslotId(mBeanList.get(0).getTimeslotid());
                rl_hra_endorse_time.setClickable(false);
                uploadType = 2;
                PrintLog.e(TAG,"===66666666666666======");
            }else {
                Drawable drawable= getResources().getDrawable(R.drawable.by_ic_home_more);
                /// 这一步必须要做,否则不会显示.
                hra_service_time.setPadding(0, 0, 0, 0);
                drawable.setBounds(0, 0, AppUtil.dip2px(16), AppUtil.dip2px(16));
                hra_service_time.setCompoundDrawables(null, null, drawable, null);
                PrintLog.e(TAG,"orderDate111=:"+Control.getOrderDate());
                if(orderDate.equals(Control.getOrderDate())){
                        hra_service_time.setText(timeslot);
                        hra_service_time.setTextColor(Color.parseColor("#FFB3B3B3"));
                        PrintLog.e(TAG,"timeslot111=:"+timeslot);
                }else{
                    hra_service_time.setText(mBeanList.get(0).getTimeslot());
                    Control.setOrderTimeslot(mBeanList.get(0).getTimeslot());
                    hra_service_time.setTextColor(Color.parseColor("#FFFBA140"));
                    PrintLog.e(TAG,"-----------------------");
                }
                Control.setOrderTimeslotId(mBeanList.get(0).getTimeslotid());
//                hra_service_time.setText(mBeanList.get(0).getTimeslot());
                rl_hra_endorse_time.setClickable(true);
                uploadType = 2;

            }
//            mBtnConfirm.setEnabled(true);
        }
    }

    //点击确认修改跳转到预约改签详情页
    @Override
    public void start2DetailActivity() {
        Intent intent = new Intent(this, MineAppointDetailActivity.class);
        intent.putExtra(MineAppointDetailActivity.MINEORDERID,mOrderId);
        intent.putExtra(MineAppointDetailActivity.ENDORSEBACK,true);
        startActivity(intent);
        finish();
    }

    @Override
    public void setBtnEnable(boolean isable) {
        mBtnConfirm.setEnabled(isable);
    }
}
